home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Columbia Kermit
/
kermit.zip
/
newsgroups
/
misc.19981211-19990422
/
000395_news@watsun.cc.columbia.edu _Mon Mar 22 02:45:55 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2020-01-01
|
4KB
Return-Path: <news@watsun.cc.columbia.edu>
Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id CAA22126
for <kermit.misc@watsun.cc.columbia.edu>; Mon, 22 Mar 1999 02:45:55 -0500 (EST)
Received: (from news@localhost)
by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id CAA16220
for kermit.misc@watsun.cc.columbia.edu; Mon, 22 Mar 1999 02:20:17 -0500 (EST)
X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
From: Mark Sapiro <msapiro@value.net>
Subject: Re: Associative Array in Kermit 95
Date: Sun, 21 Mar 1999 23:10:29 -0800
Organization: Not Very Much
Message-ID: <36F5ECE5.5E8A07D1@value.net>
To: kermit.misc@watsun.cc.columbia.edu
Frank da Cruz wrote:
>>
In article <7cttsc$47f$1@nnrp1.dejanews.com>, <dn5006@my-dejanews.com> wrote:
: Associative array is a very useful feature in scripting language, it enables
: Perl, Tcl etc. the implementation of comlex data structures. The following
: script demonstrates that associative array can also be crafted in Kermit
: 95. The script counts the unique words in a regular english, french, german,
: etc. text file.
:
: open read testfile.txt
: if fail end 1 Can't open testfile.txt
: assign \%n 0 ; init register
: while true {
: read \%l ; read each line
: if fail break ; until the end of file
: while > \flength(\%l) 0 {
: assign \%w \fbreak(\%l,{ }) ; split on space
: xif defined \m(\%w) { ; word already seen?
: _assign \%w \feval(\m(\%w) + 1) ; incr count this word
: } else {
: _assign \%w 1 ; init count this word
: increment \%n ; next register
: _assign \%n \%w ; register this word
: }
: assign \%l \fltrim(\fright(\%l,-
: \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
: }
: }
: for \%k 1 \%n 1 {
: assign \%w \m(\%k) ; get word from register
: echo <\m(\%w)> \%w ; display occurences
: }
:
: This approach avoids the use of array which has to be declared in advance.
: The script does not take into account the non alphanumeric characters.
:
: Dat Nguyen
: Airline Telecommunications and Information Services
: 770 Sherbrooke West
: Montreal, Quebec
: Canada H3A 1G1
: Email dat.nguyen&sita.int
:
Excellent! I've had associative arrays on my list for quite a while, but
the list so long and time so short. I've reformatted your script to fit
in 80 columns.
We plan to add a script library to the Kermit website -- this one will
certainly go into it. Other submissions are welcome too; send them in!
(Be sure to document the Kermit program and version and other relevant
info.)
A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
features, some of which would make it simpler and faster, for example
the new \fword() and \fsplit() functions for extracting words from strings,
with specified break masks (e.g. to make sure punctuation does not count
as part of word (e.g. "thing" and "thing."). Also you can convert each
word to lowercase with \flower() so "Thing", "thing", and "THING" count as
the same word, etc.
Readers should take special note of the "_assign" verb, which is subtly
different from "assign" (see p.457 of the manual).
- Frank
>>
There is a problem with the above approach to implementing associative
arrays. Namely, if one of the 'words' used as an index happens to be
equal to or an initial substring of a pre-defined macro (e.g. "cautious" or
"robust") or one of the macros that gets defined implicitly when certain
programming constructs are used (e.g. "break " or "continue"), the
"xif defined" test gives the "wrong" result and the word doesn't get
counted (although the macro 'word' does get (re-)defined, most likely
with an error message from \feval()).
This problem could probably be avoided by prepending some prefix to
each word before using it as a macro name.
--
Mark Sapiro <msapiro@value.net> The highway is for gamblers,
San Francisco Bay Area, California better use your sense - B. Dylan